﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using DLLTest.OpertionClass;
using DHNetSDK;
using System.Diagnostics;
using DHPlaySDK;


namespace DLLTest
{
    public partial class Frm_MultiScreen : Form
    {
        #region Private Fields

        int devChannel; // 记录通道数，从0开始
        Panel selectedPanel; // 记录所选 Panel

        //画面分割
       SplitScreen splitscreen = new SplitScreen();  
      
        int split = 0;

        /// <summary>
        /// 设备用户登录ＩＤ
        /// </summary>
        private int pLoginID;

        /// <summary>
        /// 程序消息提示Title
        /// </summary>
        private const string pMsgTitle = "大华网络SDK Demo程序";

        /// <summary>
        /// 最后操作信息显示格式
        /// </summary>
        private const string pErrInfoFormatStyle = "代码:errcode;\n描述:errmSG.";


        /// <summary>
        /// 断开连接回调
        /// </summary>
        fDisConnect disConnect;

        #endregion

        #region Constructor

        public Frm_MultiScreen()
        {
            InitializeComponent();
        }

        #endregion

        #region Event

        private void btnScreen1_Click(object sender, EventArgs e)
        {
            //测试分割画面,单画面
            this.plVideo1.Controls.Clear();//先清除Panle存在的内容。
            IList<Rectangle> rectList = splitscreen.CalcPanelRectangle(1, plVideo1.Size);
            split = 1;
            foreach (Rectangle rect in rectList)
            {
                Panel panel = new Panel();
                panel.Click += (s, arg) =>
                {
                    selectedPanel = s as Panel;
                    panel.BackColor = Color.Green;
                };
                panel.BackColor = Color.Black;
                panel.Width = rect.Width;
                panel.Height = rect.Height;
                panel.Left = rect.X;
                panel.Top = rect.Y;
                plVideo1.Controls.Add(panel);
            }
        }

        private void btnScreen4_Click(object sender, EventArgs e)
        {
            //测试分割画面,4画面
            this.plVideo1.Controls.Clear();//先清除Panle存在的内容。
            IList<Rectangle> rectList = splitscreen.CalcPanelRectangle(4, plVideo1.Size);
            split = 4;
            foreach (Rectangle rect in rectList)
            {
                Panel panel = new Panel();
                panel.Click += (s, arg) =>
                {
                    selectedPanel = s as Panel;
                    panel.BackColor = Color.Green;
                };
                panel.BackColor = Color.Black;
                panel.Width = rect.Width;
                panel.Height = rect.Height;
                panel.Left = rect.X;
                panel.Top = rect.Y;
                plVideo1.Controls.Add(panel);
            }
        }

        private void btnScreen9_Click(object sender, EventArgs e)
        {
            //测试分割画面,9画面
            this.plVideo1.Controls.Clear();//先清除Panle存在的内容。
            //this.plVideo1.Refresh();//重绘该控件得到的结果不对
            IList<Rectangle> rectList = splitscreen.CalcPanelRectangle(9, plVideo1.Size);
            split = 9;
            foreach (Rectangle rect in rectList)
            {
                Panel panel = new Panel();
                panel.Click += (s, arg) =>
                {
                    selectedPanel = s as Panel;
                    panel.BackColor = Color.Green;
                };
                panel.BackColor = Color.Black;
                panel.Width = rect.Width;
                panel.Height = rect.Height;
                panel.Left = rect.X;
                panel.Top = rect.Y;
                plVideo1.Controls.Add(panel);
            }
        }

        /// <summary>
        /// 当窗体发生了变化，一般指最大化或还原(即点击了窗体上的最大化或最小化按钮)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                //MessageBox.Show("最大化了"); 
                switch (split)
                {
                    case 1:
                        //调用btnSceern1.Click事件
                        btnScreen1.PerformClick();
                        break;
                    case 4:
                        btnScreen4.PerformClick();
                        break;
                    case 9:
                        btnScreen9.PerformClick();
                        break;
                    default:
                        break;
                }
            }
            else if (this.WindowState == FormWindowState.Normal)
            {
                //MessageBox.Show("最小化了"); 
                switch (split)
                {
                    case 1:
                        //调用btnSceern1.Click事件
                        btnScreen1.PerformClick();
                        break;
                    case 4:
                        btnScreen4.PerformClick();
                        break;
                    case 9:
                        btnScreen9.PerformClick();
                        break;
                    default:
                        break;
                }
            }
            else//最小化不予考虑
            { }
        }

        private void trView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                TreeView tree = sender as TreeView;
                if (tree == null ||
                    tree.SelectedNode == null ||
                    string.IsNullOrEmpty(tree.SelectedNode.Name) ||
                    tree.SelectedNode.Name.Length < 5 ||
                    selectedPanel == null)
                    return;

                #region 系统变量
                /// <summary>
                /// 网络设备信息
                /// </summary>
                NET_DEVICEINFO deviceInfo;

                /// <summary>
                /// 实时监视数据回调
                /// </summary>
                fRealDataCallBack cbRealData;

                /// <summary>
                /// 实时监视句柄
                /// </summary>
                //int uRealHandle;

                /// <summary>
                /// 是否己设置实时数据回调
                /// </summary>
                bool blnSetRealDataCallBack = false;
                #endregion

                deviceInfo = new NET_DEVICEINFO();
                int error = 0;
                string devIP, devPort, devUser, devPassword;
                string[] devInfor = tree.SelectedNode.Tag.ToString().Split(',');
                devIP = devInfor[0];
                devPort = devInfor[1];
                devUser = devInfor[2];
                devPassword = devInfor[3];
                devChannel = int.Parse(devInfor[4]);

                //DHPlay.DHPlayControl(PLAY_COMMAND.Stop, devChannel);
                //DHPlay.DHPlayControl(PLAY_COMMAND.CloseStream, devChannel);

                pLoginID = DHClient.DHLogin(devIP, ushort.Parse(devPort),
                    devUser, devPassword, out deviceInfo, out error);


                if (pLoginID != 0)
                {
                    int uRealHandle = DHClient.DHRealPlay(pLoginID, devChannel, selectedPanel.Handle);//只取数据不播放
//                     cbRealData = new fRealDataCallBack(cbRealDataFun);
//                     if (DHPlay.DHPlayControl(PLAY_COMMAND.OpenStream, devChannel, IntPtr.Zero, 0, (UInt32)(900 * 1024)))
//                     {
//                         //MessageBox.Show("打开流播放成功！", pMsgTitle);
//                     }
//                     else
//                     {
//                         MessageBox.Show("打开流播放失败！", pMsgTitle);
//                         return;
//                     }
// 
//                     if (DHPlay.DHSetStreamOpenMode(devChannel, PLAY_MODE.STREAME_REALTIME))//设置流播放模式
//                     {
//                         //MessageBox.Show("设置流播放模式成功！", pMsgTitle);
//                     }
//                     else
//                     {
//                         MessageBox.Show("设置流播放模式失败！", pMsgTitle);
//                         return;
//                     }
//                     if (DHPlay.DHPlayControl(PLAY_COMMAND.Start, devChannel, selectedPanel.Handle))
//                     {
//                         // MessageBox.Show("流播放开始成功！", pMsgTitle);
//                     }
//                     else
//                     {
//                         MessageBox.Show("流播放开始失败！", pMsgTitle);
//                         return;
//                     }
//                     if (blnSetRealDataCallBack == true)
//                     {
//                         return;
//                     }
//                     if (DHClient.DHSetRealDataCallBack(uRealHandle, cbRealData, IntPtr.Zero))//设置数据回调处理函数
//                     {
//                         //MessageBox.Show("设置数据回调处理函数成功！", pMsgTitle);
//                         blnSetRealDataCallBack = true;
//                     }
//                     else
//                     {
//                         MessageBox.Show("设置数据回调处理函数失败！", pMsgTitle);
//                         blnSetRealDataCallBack = false;
//                         return;
//                     }
                }

                //selectedPanel.Refresh();
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
            }
        }

        private void Frm_MultiScreen_Load(object sender, EventArgs e)
        {
            disConnect = new fDisConnect(DisConnectEvent);
            //初始化状态
            bool blnInit = DHClient.DHInit(disConnect, IntPtr.Zero);

            // 默认点击“4”
            btnScreen4.PerformClick();
        }

        #endregion

        #region Private Methods

        private void DisConnectEvent(int lLoginID, StringBuilder pchDVRIP, int nDVRPort, IntPtr dwUser)
        {
            MessageBox.Show("设备断开！", pMsgTitle);
        }

        /// <summary>
        /// 实时监视数据回调数据处理
        /// </summary>
        /// <param name="lRealHandle"></param>
        /// <param name="dwDataType"></param>
        /// <param name="pBuffer"></param>
        /// <param name="dwBufSize"></param>
        /// <param name="dwUser"></param>
        private void cbRealDataFun(int lRealHandle, UInt32 dwDataType, IntPtr pBuffer, UInt32 dwBufSize, IntPtr dwUser)
        {
            DHPlay.DHPlayControl(PLAY_COMMAND.InputData, devChannel, pBuffer, dwBufSize);//此处第二个参数nPort参数要与网络实时监控数据的取得的RealHandle对应
        }

        #endregion
    }
}
